{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "GDAL包括如下几个部分：\n",
    "+ GDALMajorObject类：带有元数据的对象。\n",
    "+ GDALDdataset类：通常是从一个栅格文件中提取的相关联的栅格波段集合和这些波段的元数据；GDALDdataset也负责所有栅格波段的地理坐标转换(georeferencing transform)和坐标系定义。\n",
    "+ GDALDriver类：文件格式驱动类，GDAL会为每一个所支持的文件格式创建一个该类的实体，来管理该文件格式。\n",
    "+ GDALDriverManager类：文件格式驱动管理类，用来管理GDALDriver类。\n",
    "\n",
    "GDAL原生支持超过100种栅格数据类型，涵盖所有主流GIS与RS数据格式，包括\n",
    "ArcInfo grids, ArcSDE raster, Imagine, Idrisi, ENVI, GRASS, GeoTIFF,HDF4, HDF5,USGS DOQ, USGS DEM,ECW, MrSID,TIFF, JPEG, JPEG2000, PNG, GIF, BMP\n",
    "\n",
    "完整的支持列表可以参考 http://www.gdal.org/formats_list.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from osgeo import gdal\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 一次性注册所有驱动\n",
    "driver = gdal.AllRegister()\n",
    "\n",
    "# 或按需注册驱动\n",
    "driver = gdal.GetDriverByName(\"GTiff\")\n",
    "driver.Register()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1670 872 4\n"
     ]
    }
   ],
   "source": [
    "# 打开已有的栅格数据\n",
    "dataset = gdal.Open(\"D:/arcgisDatasource/raster.tif\")\n",
    "# 获取栅格信息\n",
    "x = dataset.RasterXSize # 栅格数据水平像素数量\n",
    "y = dataset.RasterYSize # 栅格数据垂直像素数量\n",
    "count = dataset.RasterCount # 波段数\n",
    "print(x,y,count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(13275111.4974, 37.11689592814352, 0.0, 4233885.58117467, 0.0, -37.11689592814352)\n",
      "13275111.4974\n",
      "4233885.58117467\n",
      "37.11689592814352\n",
      "-37.11689592814352\n",
      "0.0\n",
      "0.0\n"
     ]
    }
   ],
   "source": [
    "# 获取仿射6参数\n",
    "geotransform = dataset.GetGeoTransform()\n",
    "print(geotransform)\n",
    "\n",
    "originX = geotransform[0]     # 原点x坐标\n",
    "originY = geotransform[3]     # 原点y坐标\n",
    "pixelX = geotransform[1]      # x轴像元大小\n",
    "pixelY = geotransform[5]      # y轴像元大小\n",
    "rotationX = geotransform[2]   # x轴旋转\n",
    "rotationY = geotransform[4]   # y轴旋转\n",
    "\n",
    "print(originX)\n",
    "print(originY)\n",
    "print(pixelX)\n",
    "print(pixelY)\n",
    "print(rotationX)\n",
    "print(rotationY)\n",
    "# 对于像素坐标，其与地理坐标的对应关系为\n",
    "# xOffset = int((x – originX) / pixelX)\n",
    "# yOffset = int((y – originY) / pixelY)\n",
    "# x_position = originX + pixelX * xOffset\n",
    "# y_position = originY - pixelY * yOffset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "cannot import name 'add_newdocs' from 'numpy' (D:\\Progrram Files\\anaconda\\lib\\site-packages\\numpy\\__init__.py)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-5-ab8ec2f7899b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;31m# 获取波段\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mband1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdataset\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mGetRasterBand\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;31m# darray = band1.ReadAsArray() numpy与GDAL版本不兼容\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;31m# .ReadAsArray(xOffset,yOffset,width,height)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\Progrram Files\\anaconda\\lib\\site-packages\\numpy\\__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m    140\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mloader\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mpackages\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0moptions\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    141\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 142\u001b[1;33m     \u001b[1;32mfrom\u001b[0m \u001b[1;33m.\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0madd_newdocs\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    143\u001b[0m     __all__ = ['add_newdocs',\n\u001b[0;32m    144\u001b[0m                \u001b[1;34m'ModuleDeprecationWarning'\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mImportError\u001b[0m: cannot import name 'add_newdocs' from 'numpy' (D:\\Progrram Files\\anaconda\\lib\\site-packages\\numpy\\__init__.py)"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "# 获取波段\n",
    "band1 = dataset.GetRasterBand(1)\n",
    "# darray = band1.ReadAsArray() numpy与GDAL版本不兼容\n",
    "# .ReadAsArray(xOffset,yOffset,width,height)\n",
    "band2 = dataset.GetRasterBand(2)\n",
    "band3 = dataset.GetRasterBand(3)\n",
    "band4 = dataset.GetRasterBand(4)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
